from http.client import responses

import requests

from db.db_work import MysqlLite


class UserClient:
    def __init__(self):
        self.base_url="http://127.0.0.1:8000"

    #注册功能
    def register(self,parser):
        response=requests.post(self.base_url+"/user_api/register",json=parser)
        dic=response.json()
        if dic.get('code')==200:
            return True
        else:
            return False
    #登录功能
    def login(self,parser):
        response = requests.post(self.base_url + '/user_api/login', json=parser)
        dic = response.json()
        if dic.get('code')==200:
            #登录成功，拿到用户基本信息
            self.username=parser.get('username')
            self.password=parser.get('password')
            self.is_admin=parser.get('is_admin')
        else:
            pass
        return dic.get('msg')
    #统计在线人数
    def add_online(self,username):
        response = requests.post(self.base_url + '/user_api/add_online',params={'username':username})
        return response.json()
    #用户退出登录
    def logout(self,username):
        response=requests.delete(self.base_url + '/user_api/logout',params={'username':username})
        return response.json()
    #获取单个用户
    def get_user(self,parser):
        response=requests.get('http://127.0.0.1:8000/user_api/get_user',params=parser)
        return response.json()
    #获取所有用户
    def get_all_user(self):
        response=requests.get('http://127.0.0.1:8000/user_api/get_all_user')
        return response.json()
    #修改个人信息
    def update_user(self,parser):
        response = requests.put(self.base_url+"/user_api/update_user", json=parser)
        return response.json()
    #添加商品
    def add_shop(self,parser):
        response = requests.post(self.base_url+"/shop_api/add_shop", json=parser)
        return response.json()
    #获取商品
    def get_shops(self,parser):
        response = requests.get(self.base_url+"/shop_api/get_shops", params=parser)
        return response.json()
    #修改商品信息
    def update_shop(self,parser):
        response = requests.put(self.base_url + "/shop_api/update_shop", json=parser)
        return response.json()
    #删除商品
    def delete_shop(self,parser):
        response = requests.delete('http://127.0.0.1:8000/shop_api/delete_shop', json=parser)
        return response.json()
    #添加订单
    def add_orders(self,parser):
        responses = requests.post(self.base_url+"/shop_api/post_orders", json=parser)
        return responses.json()
    #删除订单
    def delete_orders(self,parser):
        response=requests.delete('http://127.0.0.1:8000/shop_api/delete_orders',params=parser)
        return response.json().get('msg')
    #获取订单
    def get_orders(self,parser):
        responses =requests.get(self.base_url+"/shop_api/get_orders", params=parser)
        return responses.json().get('msg')
    #单商家确认发货后，数据库进行三步操作：1.将数据从订单移除，2.将数据存入购买历史，3.将商品库存减少
    def push_orders(self,parser1,parser2,parser3):
        responses2 = requests.put("http://127.0.0.1:8000/shop_api/buy_shop", json=parser2)
        if responses2.json().get('code') == 200:
            self.delete_orders(parser1)
            responses3=requests.post("http://127.0.0.1:8000/shop_api/history_buy",json=parser3)
            return True
        else:
            return False
    #获取销售有关数据
    def get_data(self,parser):
        responses = requests.get(self.base_url+"/data_analysis_api/get_data", params=parser)
        return responses.json()
    #g查找用户消费部分信息
    def get_user_some_data(self,parser):
        response = requests.get('http://127.0.0.1:8000/data_analysis_api/get_user_sum_and_data', params=parser)
        return response.json()
    #获取可视化数据（）
    def get_view(self,url,parser):
        response = requests.get(url,params=parser)
        return response.json()
    #将用户拉黑或洗白
    def update_blacklist(self,parser):
        response=requests.put(self.base_url+'/user_api/update_blacklist',json=parser)
        return response.json()
    #用户添加评论
    def set_comment(self,parser):
        response = requests.put(self.base_url+"/shop_api/set_comment", json=parser)
        return response.json()
    #查找商品评论
    def get_comments(self,parser):
        response = requests.get(self.base_url+"/shop_api/get_comment", params=parser)
        return response.json()
    #获取mo商品末日销售额
    def get_Sale_for_day(self,parser):
        response = requests.get(self.base_url+"/data_analysis_api/sales_for_days", params=parser)
        return response.json()
    #获取mo商品评分
    def get_score(self,parser):
        response = requests.get(self.base_url+"/data_analysis_api/get_score", params=parser)
        return response.json()
    #获取商品总销售额
    def get_Sale(self,parser):
        response = requests.get(self.base_url+"/data_analysis_api/sum_price", params=parser)
        return response.json()
    #用户发送消息
    def send_chat(self,parser):
        response = requests.post(self.base_url+"/user_api/send_chat", json=parser)
        return response.json()
    #获取用户消息
    def get_chat(self,parser):
        response = requests.get("http://127.0.0.1:8000/user_api/get_chat", params=parser)
        return response.json()
    #询问ai
    def query_ai(self,parser):
        response = requests.post('http://127.0.0.1:8000/ai_assistant_api/ai-test/chat', json=parser)
        if response:
            return response.json()
    #获取与ai交流的历史记录
    def get_ai_history(self,parser):
        response = requests.get(self.base_url+"/ai_assistant_api/ai-test/get_history", params=parser).json()
        lis=response.get('data')
        result=[]
        for tem in lis:
            dic={'id':tem[0],'date':tem[1],'data':tem[2]}
            result.append(dic)
        return result
    #将与ai的会话存入数据库
    def save_chat_ai(self,parser):
        response = requests.post(self.base_url+"/ai_assistant_api/ai-test/add_column", json=parser)
        return response.json()
    #在以有的基础上添加会话内容
    def add_ai_chat_history(self,parser):
        responses=requests.put(self.base_url+"/ai_assistant_api/ai-test/update_ai_chain", json=parser)
    #模糊查询商品
    def fuzzy_select_shop(self,parser):
        responses=requests.get(self.base_url+"/shop_api/fuzzy_select_shop", params=parser)
        return responses.json()
    #查询是否有管理员信息
    def exit_admin_mes(self,parser):
        responses = requests.get(self.base_url+"/user_api/select_admin_mes", params=parser)
        print(responses.json())
        return responses.json().get('data')
    #删除消息
    def delete_chat(self,parser):
        responses = requests.delete(self.base_url+"/user_api/delete_chat", params=parser)










